package android.team.app.ui.missatoll;

import android.os.Bundle;
import android.team.app.R;
import android.team.app.model.Account;
import android.team.app.ui.BaseActivity;
import android.team.app.utility.SmartStringUtility;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;

public class PersonalInformationActivity extends BaseActivity{
	private Spinner spState, spGender;
	private EditText edFirstName, edLastName, edMiddleInital,
	edCity, edZipcode, edAddress, edEmail, edPhone1, edPhone2, edPhone3, edPhone4;
	
	private CheckBox cbCheckMail;
	private ImageButton imgBtNext;
	
	private String gender, state;
	private boolean ischeckMail = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.personal_information_activity);
		
		loadUI();
		
		titleHeader.setText(getString(R.string.personal_information));
		
		loadSpecialUI();
	}
	
	private void loadSpecialUI(){
		spGender = (Spinner)findViewById(R.id.spinnerGender);
		spGender.setAdapter(new ArrayAdapter<String>(
				baseActivity, 
				android.R.layout.simple_spinner_item, 
				getResources().getStringArray(R.array.title)));
		spGender.setOnItemSelectedListener(selectTitle);
		
		
		spState = (Spinner)findViewById(R.id.spState);
		spState.setAdapter(new ArrayAdapter<String>(
				baseActivity, android.R.layout.simple_spinner_item, 
				getResources().getStringArray(R.array.states)));
		spState.setOnItemSelectedListener(selectState);
		
		edFirstName = (EditText)findViewById(R.id.edFirstName);
		edLastName = (EditText)findViewById(R.id.edLastName);
		edMiddleInital = (EditText)findViewById(R.id.edMiddleInital);
		edAddress = (EditText)findViewById(R.id.edAddress);
		edCity = (EditText)findViewById(R.id.edCity);
		edZipcode = (EditText)findViewById(R.id.edZipcode);
		edEmail = (EditText)findViewById(R.id.edEMail);
		
		edPhone1 = (EditText)findViewById(R.id.edPhone1);
		edPhone2 = (EditText)findViewById(R.id.edPhone2);
		edPhone3 = (EditText)findViewById(R.id.edPhone3);
		edPhone4 = (EditText)findViewById(R.id.edPhone4);
		
		cbCheckMail = (CheckBox)findViewById(R.id.cbEmail);
		cbCheckMail.setOnCheckedChangeListener(checkChanged);
		imgBtNext = (ImageButton)findViewById(R.id.btNext);
		imgBtNext.setOnClickListener(onClickNext);
	}

	OnClickListener onClickNext = new OnClickListener() {
		@Override
		public void onClick(View v) {
			//tai se kiem tra tat ca dau vao (Validate)
			//neu du lieu ok --> se luu lai va chuyen sang giao dien khac
			if(checkValidate()){
				//dung doi tuong account de luu tru tat ca thong tin ma nguoi dung nhap vao
				Account account = new Account();
				
				account.setGener(gender);
				account.setFisrtname(edFirstName.getText().toString().trim());
				account.setMiddlename(edMiddleInital.getText().toString().trim());
				account.setLastname(edLastName.getText().toString().trim());
				account.setCity(edCity.getText().toString().trim());
				account.setZipcode(edZipcode.getText().toString().trim());
				account.setState(state);
				account.setAddress(edAddress.getText().toString().trim());
				account.setMail(edEmail.getText().toString().trim());
				account.setMail_receipt(ischeckMail);
				account.setPhone(edPhone1.getText().toString().trim() + edPhone2.getText().toString().trim()+ edPhone3.getText().toString().trim());
				account.setPhoneExt(edPhone4.getText().toString().trim());
				
				application.setAccount(account);
				
				
				
			}
		}
	};

	OnCheckedChangeListener checkChanged = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			ischeckMail = isChecked;
		}
	};
	
	OnItemSelectedListener selectState = new OnItemSelectedListener() {
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,	int pos, long id) {
			state = parent.getItemAtPosition(pos).toString();
		}
		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			state = "AK";
		}
	};
	OnItemSelectedListener selectTitle = new OnItemSelectedListener() {
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,	int pos, long id) {
			gender = parent.getItemAtPosition(pos).toString();
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			gender = "Mr";
		}
	};
 
	//day la phuong thuc check xem du lieu nhap vao chua va nhap dung dinh dang hay khong?

	private boolean checkValidate(){
		if(SmartStringUtility.isEmpty(edFirstName)){
			edFirstName.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}else if(SmartStringUtility.isEmpty(edLastName)){
			edLastName.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}
		else if(SmartStringUtility.isEmpty(edAddress)){
			edAddress.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}else if(SmartStringUtility.isEmpty(edCity)){
			edCity.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}else if(SmartStringUtility.isEmpty(edZipcode)){
			edZipcode.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}else if(SmartStringUtility.isEmpty(edEmail)){
			edEmail.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}else if(!SmartStringUtility.checkEmail(edEmail.getText().toString())){
			edEmail.setBackgroundResource(android.R.drawable.editbox_background_normal);
			return false;
		}
		else if((!SmartStringUtility.isEmpty(edPhone1)&&SmartStringUtility.isEmpty(edPhone2)&&SmartStringUtility.isEmpty(edPhone3))||
			(SmartStringUtility.isEmpty(edPhone1)&&!SmartStringUtility.isEmpty(edPhone2)&&SmartStringUtility.isEmpty(edPhone3))||
			(SmartStringUtility.isEmpty(edPhone1)&&SmartStringUtility.isEmpty(edPhone2)&&!SmartStringUtility.isEmpty(edPhone3))||
			(!SmartStringUtility.isEmpty(edPhone4)&&SmartStringUtility.isEmpty(edPhone1)&&SmartStringUtility.isEmpty(edPhone2)&&SmartStringUtility.isEmpty(edPhone3))||
			(!SmartStringUtility.isEmpty(edPhone1)&&!SmartStringUtility.isEmpty(edPhone2)&&!SmartStringUtility.isEmpty(edPhone3)&&!SmartStringUtility.isEmpty(edPhone4)&&
			((edPhone1.getText().length()!=3)||(edPhone2.getText().length()!=3)||(edPhone3.getText().length()!=4)||(edPhone4.getText().length()!=3)))){
			
			edPhone1.setBackgroundResource(R.drawable.custom_boder);
			edPhone2.setBackgroundResource(R.drawable.custom_boder);
			edPhone3.setBackgroundResource(R.drawable.custom_boder);
			edPhone4.setBackgroundResource(R.drawable.custom_boder);
			return false;
		}
		
		return true;
	}

}
